package com.chenbei.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenbei.erp.domain.SupplierContact;
import com.chenbei.erp.mapper.SupplierContactMapper;
import com.chenbei.erp.service.ISupplierContactService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Service
public class SupplierContactService extends ServiceImpl<SupplierContactMapper, SupplierContact> implements ISupplierContactService {

    @Transactional
    @Override
    public void batchAdd(Long supplierId, List<SupplierContact> contacts) {
        LambdaQueryWrapper<SupplierContact> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SupplierContact::getSupplierId, supplierId);
        remove(lambdaQueryWrapper);

        if (contacts != null && !contacts.isEmpty()) {
            for (SupplierContact contact : contacts) {
                contact.setSupplierId(supplierId);
            }
            saveBatch(contacts);
        }
    }

    @Override
    public List<SupplierContact> listBySupplierId(Long supplierId) {
        LambdaQueryWrapper<SupplierContact> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SupplierContact::getSupplierId, supplierId);
        return list(lambdaQueryWrapper);
    }
}
